var CaneclerLunar  = function(domid){
    

    let canecler_lunar_html = '<div class="canecle_desc">'+
    '<div class="desc_chunk_top">'+
    '<div class="cell_2 chunk_top_left">'+
    '<span class="spanblock" id="date"></span>'+
    '<span class="spanblock" id="date_cn"></span>'+
    '<span class="spanblock" id="date_cn_desc"></span>'+
    '</div>'+
    '<div class="cell_1 chunk_top_right">'+
    '<span class="spanblock" id="week">星 期</span>'+
    '<span class="spanblock" id="week_day"></span>'+
    '</div>'+
    '</div>'+
    '<div class="desc_chunk_under">'+
    '<div class="cell_1 chunk_under_left">'+
    '<span class="spanblock" id="jieri">节 日</span>'+
    '<ul id="jieri_desc">'+
    '</ul>'+
    '</div>'+
    '<div class="cell_2 chunk_under_right">'+
    '<div class="under_right_top">'+
    '<div class="cell_1 right_top_yi">'+
    '<span class="spanblock yiji" id="yi">宜</span>'+
    '<ul class="yiji_list" id="yi_list">'+
    '</ul>'+
    '</div>'+
    '<div class="cell_1 right_top_ji">'+
    '<span class="spanblock yiji" id="ji">忌</span>'+
    '<ul class="yiji_list" id="ji_list">'+
    '</ul>'+
    '</div>'+
    '</div>'+
    '<div class="under_right_under">'+
    '<table class="jieri_countdown">'+
    '</table>'+
    '</div>'+
    '</div>'+
    '</div>'+
    '</div>'
    $("#" + domid).append(canecler_lunar_html)

     //获取日历详情
    function GetCalendar(y, m, d) {
        let sol = Solar.fromDate(new Date('{0}-{1}-{2}'.format(y, m, d)))
        let lun = Lunar.fromDate(new Date('{0}-{1}-{2}'.format(y, m, d)))
        let res = {
            date: "",
            cn_date: "",
            cn_year: "",
            cn_month: "",
            cn_day: "",
            weekday: "",
            jieri: [],
            yi: [],
            ji: []
        }
        res.date = "{0}年{1}月{2}日".format(sol.getYear(), sol.getMonth(), sol.getDay())
        res.cn_date = "农历{0}月{1}".format(lun.getMonthInChinese(), lun.getDayInChinese())
        if (lun.getJieQi() != '') {
            res.cn_date += "({0})".format(lun.getJieQi())
        }
        res.cn_year = "{0}{1}年".format(lun.getYearInGanZhi(), lun.getYearShengXiao())
        res.cn_month = "{0}月".format(lun.getMonthInGanZhi())
        res.cn_day = "{0}日".format(lun.getDayInGanZhi())
        res.weekday = lun.getWeekInChinese()
        res.jieri = sol.getFestivals().concat(sol.getOtherFestivals()).concat(lun.getFestivals())
        res.yi = lun.getDayYi()
        res.ji = lun.getDayJi()
        return res
    }

     //把日历详情放入页面
     function DrawView(y, m, d) {
        let lun_desc = GetCalendar(y, m, d)
        let date = $("#date")
        let date_cn = $("#date_cn")
        let date_cn_desc = $("#date_cn_desc")
        let week_day = $("#week_day")
        let jieri_desc = $("#jieri_desc")
        let yi_list = $("#yi_list")
        let ji_list = $("#ji_list")
        let jieri_countdown = $(".jieri_countdown")
        date.html(lun_desc.date)
        date_cn.html(lun_desc.cn_date)
        date_cn_desc.html("{0}•{1}•{2}".format(lun_desc.cn_year, lun_desc.cn_month, lun_desc.cn_day))
        week_day.html(lun_desc.weekday)

        let jieri_template =
            "<li title='{0}'><marquee behavior='scroll' scrollamount='2'>{0}</marquee></li>"
        let jieri_template1 = "<li title='{0}'>{0}</li>"
        let jieri_str = ""
        if (lun_desc.jieri.length > 0) {
            lun_desc.jieri.forEach(e => {
                if (e.length > 5) {
                    jieri_str += jieri_template.format(e)
                } else {
                    jieri_str += jieri_template1.format(e)
                }

            });
        } else {
            jieri_str = jieri_template.format("今天没有节日!")
        }
        jieri_desc.find("li").remove()
        jieri_desc.append(jieri_str)

        let yiji_template = "<li title='{0}'>{0}</li>"
        let yi_str = ""
        if (lun_desc.yi.length > 0) {
            lun_desc.yi.forEach(e => {
                yi_str += yiji_template.format(e)
            });
        }
        yi_list.find("li").remove()
        yi_list.append(yi_str)

        let ji_str = ""
        if (lun_desc.ji.length > 0) {
            lun_desc.ji.forEach(e => {
                ji_str += yiji_template.format(e)
            });
        }
        ji_list.find("li").remove()
        ji_list.append(ji_str)


        //获取有假日的节日并放入界面
        let daojishi = holiday()
        let djs_template = "<tr><td>距离</td><td>{0}</td><td>还剩</td><td>{1}</td><td>天</td></tr>"
        let djs_str = ""
        daojishi.forEach(e => {
            djs_str += djs_template.format(e.name, e.diffDay)
        });
        jieri_countdown.find('tr').remove()
        jieri_countdown.append(djs_str)
    }

      //主要假期节日
      function holiday() {
        let date = new Date()
        let d = Lunar.fromYmd(date.getFullYear(), 1, 1)
        let hol = [{
                'name': '元旦',
                'time_year': date.getFullYear().toString(),
                'time_month': '01',
                'time_day': '01'
            },
            {
                'name': '春节',
                'time_year': date.getFullYear().toString(),
                'time_month': Lunar.fromYmd(date.getFullYear(), 1, 1).getSolar().getMonth().toString(),
                'time_day': Lunar.fromYmd(date.getFullYear(), 1, 1).getSolar().getDay().toString()
            }, {
                'name': '清明节',
                'time_year': d.getJieQiTable()['清明'].toYmd().toString().split('-')[0],
                'time_month': d.getJieQiTable()['清明'].toYmd().toString().split('-')[1],
                'time_day': d.getJieQiTable()['清明'].toYmd().toString().split('-')[2]
            }, {
                'name': '劳动节',
                'time_year': date.getFullYear().toString(),
                'time_month': '05',
                'time_day': '01'
            }, {
                'name': '端午节',
                'time_year': date.getFullYear().toString(),
                'time_month': Lunar.fromYmd(date.getFullYear(), 5, 5).getSolar().getMonth().toString(),
                'time_day': Lunar.fromYmd(date.getFullYear(), 5, 5).getSolar().getDay().toString()
            }, {
                'name': '中秋节',
                'time_year': date.getFullYear().toString(),
                'time_month': Lunar.fromYmd(date.getFullYear(), 8, 15).getSolar().getMonth().toString(),
                'time_day': Lunar.fromYmd(date.getFullYear(), 8, 15).getSolar().getDay().toString()
            }, {
                'name': '国庆节',
                'time_year': date.getFullYear().toString(),
                'time_month': '10',
                'time_day': '01'
            }
        ]

        let time = []
        hol.forEach(e => {
            let dates = "{0}-{1}-{2}".format(e.time_year, e.time_month, e.time_day)
            let datess = new Date(dates)
            let i = 0
            if (datess > date) {
                if (i < 3) {
                    let diffDay = parseInt((datess - date) / (1000 * 60 * 60 * 24))
                    let obj = {
                        'name': e.name,
                        'diffDay': diffDay
                    }
                    time.push(obj)
                }
                i++
            }
        });

        if (time.length < 3) {
            hol.forEach(e => {
                let dates = "{0}-{1}-{2}".format(parseInt(e.time_year) + 1, e.time_month, e.time_day)
                let datess = new Date(dates)
                i = 3 - time.length
                if (i > 0) {
                    let diffDay = parseInt((datess - date) / (1000 * 60 * 60 * 24))
                    let obj = {
                        'name': e.name,
                        'diffDay': diffDay
                    }
                    time.push(obj)
                }
                i--
            });
        }
        return time
    }

    let dddd = new Date()
    window.onload = DrawView(dddd.getFullYear(),dddd.getMonth()+1,dddd.getDate())
}